package com.bite.demo.controller;

import com.bite.demo.model.UserInfo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/response")
//@Controller 理解为表示, 下面的类需要交给Spring, 返回视图
//@RestController = @Controller + @ResponseBody
//@ResponseBody 表示返回数据, 可以修饰类和方法
@Controller
public class ResponseController {
    //访问路径: /index.html 或 /response/index
    @RequestMapping("/index")
    public String index(){
        System.out.println("1");
        return "/index.html";
    }

    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData(){
        return "返回数据";
    }
    @ResponseBody
    //返回数据是HTML, 可以识别HTML数据
    @RequestMapping("/returnHtml")
    public String returnHtml(){
        return "<h1>我是HTML页面</h1>";
    }
    @ResponseBody
    //返回数据是对象, 可以返回成JSon给前端
    @RequestMapping("/returnJson")
    public UserInfo returnJson(){
        UserInfo userInfo = new UserInfo("张三","123456", 111);
        return userInfo;
    }
    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(401);
        return "设置状态码成功";
    }


}
